package com.xinqi.modules.exam.analyze.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.exam.analyze.domain.CourseExamPaperAnalyzeEntity;
import com.xinqi.modules.exam.analyze.dto.req.CourseExamAnalyzeStudentScoreQuestDTO;
import com.xinqi.modules.exam.analyze.dto.req.CourseExamPaperAnalyzeQueryDTO;
import com.xinqi.modules.exam.analyze.dto.rsp.CourseExamAnalyzeStudentScoreDTO;
import com.xinqi.modules.exam.analyze.dto.rsp.CourseExamPaperAnalyzeResultDTO;
import com.xinqi.modules.exam.analyze.manager.CourseExamPaperAnalyzeManager;
import com.xinqi.modules.exam.analyze.mapper.CourseExamPaperAnalyzeMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 课程测评分析管理层实现
 *
 * @author: linkai
 * @date: 2023/07/02
 */
@Service
@RequiredArgsConstructor
public class CourseExamPaperAnalyzeManagerImpl extends BaseServiceImpl<CourseExamPaperAnalyzeMapper, CourseExamPaperAnalyzeEntity> implements CourseExamPaperAnalyzeManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<CourseExamPaperAnalyzeResultDTO> findPage(Page<Object> page, CourseExamPaperAnalyzeQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<CourseExamPaperAnalyzeResultDTO> findList(CourseExamPaperAnalyzeQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 课程测评分析数据
     */
    @Override
    public CourseExamPaperAnalyzeEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id更新 课程测评分析数据，重写
     */
    @Override
    public boolean updateById(CourseExamPaperAnalyzeEntity entity) {
        return SqlHelper.retBool(getBaseMapper().updateById(entity));
    }

    /**
     * 根据id删除 课程测评分析数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public CourseExamPaperAnalyzeEntity findByExamId(Long examId) {
        LambdaQueryWrapper<CourseExamPaperAnalyzeEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseExamPaperAnalyzeEntity::getExamPaperId, examId);
        return getOne(queryWrapper);
    }

    @Override
    public IPage<CourseExamAnalyzeStudentScoreDTO> findStudentScore(Page<Object> page, CourseExamAnalyzeStudentScoreQuestDTO questDTO) {
        return this.baseMapper.findStudentScore(page, questDTO);
    }

}
